﻿using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using LeekGPTApp.Library.Services;
using SemanticKernelBlazorDemo.Shared.Services;


namespace LeekGPTApp.Library.ViewModels;

public class MainPageViewModel : ObservableObject{
    private bool _inputVisible = true;
    public bool InputVisible
    {
        get => _inputVisible;
        set => SetProperty(ref _inputVisible, value);
    }

    private bool _resultVisible = false;
    public bool ResultVisible
    {
        get => _resultVisible;
        set => SetProperty(ref _resultVisible, value);
    }



    private string _result = string.Empty;
    public string Result {
        get => _result;
        set => SetProperty(ref _result, value);
    }

    private string _classfiedKeys = string.Empty;
    public string ClassfiedKeys
    {
        get => _classfiedKeys;
        set => SetProperty(ref _classfiedKeys, value);
    }

    private string _fundsIntro = string.Empty;
    public string FundsIntro
    {
        get => _fundsIntro;
        set => SetProperty(ref _fundsIntro, value);
    }

    private string _evaluateResult = string.Empty;
    public string EvaluateResult
    {
        get => _evaluateResult;
        set => SetProperty(ref _evaluateResult, value);
    }
   
    private string stock0 = string.Empty;
    public string Stock0
    {
        get => stock0;
        set => SetProperty(ref stock0, value);
    }

    private string stock1 = string.Empty;
    public string Stock1
    {
        get => stock1;
        set => SetProperty(ref stock1, value);
    }

    private string stock2 = string.Empty;
    public string Stock2
    {
        get => stock2;
        set => SetProperty(ref stock2, value);
    }

    private string stock3 = string.Empty;
    public string Stock3
    {
        get => stock3;
        set => SetProperty(ref stock3, value);
    }

    private string stock4 = string.Empty;
    public string Stock4
    {
        get => stock4;
        set => SetProperty(ref stock4, value);
    }

    private string stock5 = string.Empty;
    public string Stock5
    {
        get => stock5;
        set => SetProperty(ref stock5, value);
    }

    private string stock6 = string.Empty;
    public string Stock6
    {
        get => stock6;
        set => SetProperty(ref stock6, value);
    }

    private string stock7 = string.Empty;
    public string Stock7
    {
        get => stock7;
        set => SetProperty(ref stock7, value);
    }

    private string stock8 = string.Empty;
    public string Stock8
    {
        get => stock8;
        set => SetProperty(ref stock8, value);
    }

    private string stock9 = string.Empty;
    public string Stock9
    {
        get => stock9;
        set => SetProperty(ref stock9, value);
    }


    private string stockid0 = string.Empty;
    public string Stockid0
    {
        get => stockid0;
        set => SetProperty(ref stockid0, value);
    }

    private string stockid1 = string.Empty;
    public string Stockid1
    {
        get => stockid1;
        set => SetProperty(ref stockid1, value);
    }

    private string stockid2 = string.Empty;
    public string Stockid2
    {
        get => stockid2;
        set => SetProperty(ref stockid2, value);
    }

    private string stockid3 = string.Empty;
    public string Stockid3
    {
        get => stockid3;
        set => SetProperty(ref stockid3, value);
    }

    private string stockid4 = string.Empty;
    public string Stockid4
    {
        get => stockid4;
        set => SetProperty(ref stockid4, value);
    }

    private string stockid5 = string.Empty;
    public string Stockid5
    {
        get => stockid5;
        set => SetProperty(ref stockid5, value);
    }

    private string stockid6 = string.Empty;
    public string Stockid6
    {
        get => stockid6;
        set => SetProperty(ref stockid6, value);
    }

    private string stockid7 = string.Empty;
    public string Stockid7
    {
        get => stockid7;
        set => SetProperty(ref stockid7, value);
    }

    private string stockid8 = string.Empty;
    public string Stockid8
    {
        get => stockid8;
        set => SetProperty(ref stockid8, value);
    }

    private string stockid9 = string.Empty;
    public string Stockid9
    {
        get => stockid9;
        set => SetProperty(ref stockid9, value);
    }



    private string stockidper0 = string.Empty;
    public string Stockidper0
    {
        get => stockidper0;
        set => SetProperty(ref stockidper0, value);
    }

    private string stockidper1 = string.Empty;
    public string Stockidper1
    {
        get => stockidper1;
        set => SetProperty(ref stockidper1, value);
    }

    private string stockidper2 = string.Empty;
    public string Stockidper2
    {
        get => stockidper2;
        set => SetProperty(ref stockidper2, value);
    }

    private string stockidper3 = string.Empty;
    public string Stockidper3
    {
        get => stockidper3;
        set => SetProperty(ref stockidper3, value);
    }

    private string stockidper4 = string.Empty;
    public string Stockidper4
    {
        get => stockidper4;
        set => SetProperty(ref stockidper4, value);
    }

    private string stockidper5 = string.Empty;
    public string Stockidper5
    {
        get => stockidper5;
        set => SetProperty(ref stockidper5, value);
    }

    private string stockidper6 = string.Empty;
    public string Stockidper6
    {
        get => stockidper6;
        set => SetProperty(ref stockidper6, value);
    }

    private string stockidper7 = string.Empty;
    public string Stockidper7
    {
        get => stockidper7;
        set => SetProperty(ref stockidper7, value);
    }

    private string stockidper8 = string.Empty;
    public string Stockidper8
    {
        get => stockidper8;
        set => SetProperty(ref stockidper8, value);
    }

    private string stockidper9 = string.Empty;
    public string Stockidper9
    {
        get => stockidper9;
        set => SetProperty(ref stockidper9, value);
    }


    private readonly ISelectKeyService _selectKeyService;
    private readonly IJudgeKeyService _judgeKeyService;
    private readonly IEvaluateService _evaluateService;
    private readonly IGPTEvaluateService _gptEvaluateService;
    private readonly IHtmlInfoService _htmlInfoService;
    private readonly IFundInfo _fundInfo;
    public MainPageViewModel(ISelectKeyService selectKeyService, IJudgeKeyService judgeKeyService, 
        IEvaluateService evaluateService, IGPTEvaluateService gptEvaluateService, 
        IHtmlInfoService htmlInfoService,IFundInfo fundInfoService) {

        _selectKeyService = selectKeyService;
        _judgeKeyService = judgeKeyService;
        _evaluateService = evaluateService;
        _htmlInfoService = htmlInfoService;
        _gptEvaluateService = gptEvaluateService;
        _fundInfo = fundInfoService;

        _lazyShowResultCommand = new Lazy<AsyncRelayCommand>(() =>
            new AsyncRelayCommand(ShowResultCommandFunction));
        _lazyReturnCommand = new Lazy<AsyncRelayCommand>(() =>
            new AsyncRelayCommand(ReturnCommandFunction));
        _lazyResultCommand = new Lazy<AsyncRelayCommand>(() =>
            new AsyncRelayCommand(ResultCommandFunction));
    }
    public AsyncRelayCommand ReturnCommand => _lazyReturnCommand.Value;
    private readonly Lazy<AsyncRelayCommand> _lazyReturnCommand;

    public async Task ReturnCommandFunction() {
        InputVisible = true;
        ResultVisible = false;
    }
    public AsyncRelayCommand ResultCommand => _lazyResultCommand.Value;
    private readonly Lazy<AsyncRelayCommand> _lazyResultCommand;

    public async Task ResultCommandFunction()
    {
        InputVisible = false;
        ResultVisible = true;
    }

    public AsyncRelayCommand ShowResultCommand => _lazyShowResultCommand.Value;
    private readonly Lazy<AsyncRelayCommand> _lazyShowResultCommand;
    public async Task ShowResultCommandFunction()
    {
        //List<List<Dictionary<string, object>>> list = await _fundInfo.getIn("161725");
        //FundInfo fundinfo = new FundInfo(list);
        //获得目标股票的id集合
        List<string> stockids = new List<string>();
        stockids.Add(stockid0);
        stockids.Add(stockid1);
        stockids.Add(stockid2);
        stockids.Add(stockid3);
        stockids.Add(stockid4);
        stockids.Add(stockid5);
        stockids.Add(stockid6);
        stockids.Add(stockid7);
        stockids.Add(stockid8);
        stockids.Add(stockid9);
        //获得目标股票占比集合
        List<String> stockpers = new List<string>();
        stockpers.Add(stockidper0);
        stockpers.Add(stockidper1);
        stockpers.Add(stockidper2);
        stockpers.Add(stockidper3);
        stockpers.Add(stockidper4);
        stockpers.Add(stockidper5);
        stockpers.Add(stockidper6);
        stockpers.Add(stockidper7);
        stockpers.Add(stockidper8);
        stockpers.Add(stockidper9);
        String keys;
        String keys_judged;
        int stockcount = 0;
        int stockvalue = 0;
        foreach (string stockid in stockids)
        {
            if (!stockid.Equals(string.Empty))
            {
                double stockscore = 0;
                int count = 0;
                String text = await _htmlInfoService.getInfoAsync(stockid);
                String[] texts = text.Split("*******");
                foreach(String str in texts){
                    if (!str.Equals(string.Empty)){
                        count++;
                         keys = _selectKeyService.SelectKeyAsync(texts[0]).ToString();
                        //将获得的关键词句进行分类
                         keys_judged = _judgeKeyService.JudgeKeyeAsync(texts[0]).ToString();
                        ClassfiedKeys = keys_judged;
                        // 依据分类进行评估
                        stockscore+=_evaluateService.Evaluate(keys_judged);
                    }
                }
                //该股分数
                stockscore = stockscore / count;
                //利用比例计算总分数
                stockvalue += stockvalue * (int.Parse(stockpers[stockcount]))/100;
            }
            stockcount++;
        }

        Result=stockvalue.ToString();
        if (string.Empty.Equals(FundsIntro))
        {
            return;
        }

        var result = _gptEvaluateService.EvaluateAsync(FundsIntro);

        EvaluateResult = result;

        InputVisible = false;
        ResultVisible = true;
    }
}

